<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>全选/全不选</title>
</head>

<body>
    <h1>你喜欢的水果:</h1>
    苹果:<input type="checkbox" name="fruit" value="苹果" /> 香蕉:
    <input type="checkbox" name="fruit" value="香蕉" /> 葡萄:
    <input type="checkbox" name="fruit" value="葡萄" /> 菠萝:
    <input type="checkbox" name="fruit" value="菠萝" />
    <button id="all">全选</button>
    <button id="not">全不选</button>
    <button id="toggle">反选</button>
    <script>
        //全选
        let all = document.getElementById("all");
        let fruit = document.getElementsByName("fruit");
        all.onclick = function() {
                for (let i = 0; i < fruit.length; i++) {
                    fruit[i].checked = true
                }
            }
            //反选
        let not = document.getElementById("not");
        not.onclick = function() {
                for (let i = 0; i < fruit.length; i++) {
                    fruit[i].checked = false
                }
            }
            //反选
        let toggle = document.getElementById("toggle");
        toggle.onclick = function() {
            for (let i = 0; i < fruit.length; i++) {
                /*if(fruit[i].checked){
                    fruit[i].checked=false
                }else{
                    fruit[i].checked=true
                }*/
                fruit[i].checked = !fruit[i].checked

            }
        }
    </script>
</body>

</html>